<?php
/**
 * Created by PhpStorm.
 * User: lizx
 * Date: 2018/2/7
 * Time: 11:30
 */

namespace app\api\service;

use app\common\Model\Freight;
use app\common\Model\OrderActionShaohuo;
use app\common\Model\OrderInfoShaohuo;
use app\common\Model\Region;
use app\common\Model\User;
use app\common\Model\UserAddress;
use app\common\Server\Redis;
use app\common\Service;
use think\Config;

class ShaohuoOrderService extends Service
{
    //操作类型对应描述信息
    private $action_type = [
        '1' => '[name1] 提交了订单',
        '2' => '[name1] 已完成付款',
        '3' => '[name1] 取消了订单',
        '4' => '捎货人 [name2]([phone2])已接单，请耐心等待取货',
        '5' => '捎货人 [name2]([phone2])已取件，正在送货途中，请准备收货',
        '6' => '捎货人 [name2]([phone2])已完成送货，收件人[name3]',
        '7' => '[name1] 已确认签收，感谢您使用捎货，期待再次为您服务',
        '8' => '[name2] 已确认服务，感谢您使用捎货，期待再次为您服务',
    ];

    /**
     * 运费计算
     * @return bool
     */
    public function shippingFee($fc_id,$ua_id)
    {
        if($fc_id != 1 && $fc_id != 2){
            self::$data['shipping_fee'] = '0.00';
            return true;
        }

        $addr = UserAddress::get($ua_id);
        $area_region_id = Region::get(['name' => $addr->district]);
        $region_price = Freight::get(['area_region_id' => $area_region_id->region_id ?? 0]);
        if(!$region_price){
            $city_region_id = Region::get(['name' => $addr->city]);
            $region_price = Freight::get(['city_region_id' => $city_region_id->region_id ?? 0]);
            if(!$region_price){
                $province_region_id = Region::get(['name' => $addr->province]);
                $region_price = Freight::get(['province_region_id' => $province_region_id->region_id ?? 0]);
            }
        }

        $price = $region_price->price ?? 0;

        self::$data['shipping_fee'] = sprintf("%.2f",$price / 100);
        return true;
    }

    /**
     * 生成订单路由信息 在上层事务内执行
     * @param $order_id
     * @param $type 操作类型1提交订单2完成付款3取消订单4接单5取件6已送达7已签收(完成)8确认服务
     * @param $deal_uid
     * @param string $commit
     */
    public function orderAction($order_id,$type,$deal_uid,$commit = '',$lat = '',$lng = '')
    {
        //暂时只有帮送业务添加路由信息
//        if($func_id != 2){
//            return true;
//        }
        //查询用户信息
        $deal_user = User::get($deal_uid);

        //操作人 优先真实姓名 其次昵称 再次 手机号
        $name = ($name = $deal_user->real_name ? $deal_user->real_name : $deal_user ->nick_name) ? $name : $deal_user->mobile;

        $name1 = '';
        $name2 = '';
        $phone2 = '';
        $name3 = '';

        if(in_array($type,[1,2,3,7])){
            $name1 = $name;
        }else{
            $name2 = $name;
            $phone2 = $deal_user->mobile;
            $name3 = $commit;
        }

        $desc = str_replace('[name1]',$name1,$this->action_type[$type]);
        $desc = str_replace('[name2]',$name2,$desc);
        $desc = str_replace('[phone2]',$phone2,$desc);
        $desc = str_replace('[name3]',$name3,$desc);

        //保存信息
        $data = [
            'order_id' => $order_id,
            'type' => $type,
            'desc' => $desc,
            'deal_uid' => $deal_uid,
            'commit' => $commit,
            'lat' => $lat,
            'lng' => $lng
        ];

        $rtn = OrderActionShaohuo::get([
            'order_id' => $order_id,
            'type' => $type,
            'deal_uid' => $deal_uid,
        ]);
        if(empty($rtn)){
            OrderActionShaohuo::create($data);
        }
    }

    /**
     * 获取路径信息
     * @param $order_id
     * @return mixed
     */
    public function orderMap($order_id)
    {
        $order_info = OrderInfoShaohuo::get(['order_id' => $order_id]);
        $shipping = User::get(['uid' => $order_info->shipping_uid])->toArray();
        $shipping_user['mobile'] = $shipping['mobile'];
        $shipping_user['name'] = $shipping['real_name'] ? $shipping['real_name'] : $shipping['nick_name'];
        $shipping_user['header_img'] = Config::get('aliyun_oss.img_url') . $shipping['header_img'];

        $user_redis = 'CACHE:USER:LOCATE:' . $order_info->shipping_uid;
        $redis = new Redis();
        $hredis = $redis->handler();
        $lat = $hredis->hget($user_redis,'lat');
        $lng = $hredis->hget($user_redis,'lng');
        //todo 查询当前地理位置  redis  和捎货人app一致
        $shipping_user['start']['lat'] = empty($lat) ? '30.1884688' : $lat;
        $shipping_user['start']['lng'] = empty($lng) ? '120.2015955' : $lng;

        $end_addr = json_decode($order_info->end_addr,true);
        $shipping_user['end']['lat'] = $end_addr['lat'] ?? '';
        $shipping_user['end']['lng'] = $end_addr['lng'] ?? '';

        $distance = BaiduMapService::instance()->getDistance($shipping_user['start']['lat'],$shipping_user['start']['lng'],
            $shipping_user['end']['lat'],$shipping_user['end']['lng']);
        $shipping_user['remaining_time'] = $distance['remaining_time'];//剩余配送时间

        $shipping_user['start'] = BaiduMapService::instance()->getSiteInfo( $shipping_user['start']['lat'],$shipping_user['start']['lng']);
        $shipping_user['end'] = BaiduMapService::instance()->getSiteInfo( $shipping_user['end']['lat'],$shipping_user['end']['lng']);

        $shipping_user['start']['lat'] = empty($lat) ? '30.1884688' : $lat;
        $shipping_user['start']['lng'] = empty($lng) ? '120.2015955' : $lng;
        $shipping_user['end']['lat'] = $end_addr['lat'] ?? '';
        $shipping_user['end']['lng'] = $end_addr['lng'] ?? '';
        return $shipping_user;
    }


}